home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FAQ.SWG / 0034_MODE-X Information.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  19KB  |  332 lines

  1.  
  2. ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  3. ■■■■■■■■    SyNeRgY DeSiGn presents a production by LORD HELMET    ■■■■■■■■
  4. ■■■■■■■■                        MODE X                             ■■■■■■■■
  5. ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  6.  
  7. The purpose of this file is to provide the information necessary for using 
  8. mode x in your programs. Some knowledge of the VGA card and assembly program-
  9. ming are required to understand this text, although I'll try to make it as
  10. complete and easy as possible. I was working on a textfile about 3D graphics,
  11. but this will be delayed for a while since I started experimenting with the
  12. VGA card some time ago. This file should be an even greater help for coders
  13. so it will surely make up for this delay.
  14.  
  15. What is mode x ?
  16. ----------------
  17.  
  18. Now there's a difficult question and to be honest, I can't really answer it.
  19. Ask this question to ten coders and you will get at least five different 
  20. answers. The problem is : mode x is non-standard. In my opinion it's the
  21. ordinary mode 13 hex (320x200x256) with some registers altered adding some pos-
  22. sibilities to the normal graphics output. Mode x is a great help for programs
  23. that require fast graphics output and it's excellent for scrollers. Before I go
  24. on I will explain some things about mode 13h and the VGA card in general. If
  25. you know the VGA card well enough you can skip this part, but don't go thinking
  26. "hey, I don't need this VGA crap" too easily, because the clues to mode x are
  27. included in the following paragraph, so I would surely advise you to read it.
  28.  
  29. The VGA card & mode 13h
  30. -----------------------
  31.  
  32. As you may know by now, the VGA card's memory is mapped into the computer's
  33. central memory at segments A000h & B000h. In most modes, these segments aren't
  34. both used at the same time, but one's used for color output (segment A000h),
  35. whereas the other is used for monochrome output (segment B000h), allowing more
  36. than one graphics card to be present in your computer. Segment B000h isn't nor-
  37. mally used in mode 13h with color output (there are some VGA cards which do 
  38. allow the use of the second segment, such as the Trident cards), so forget
  39. about that one. Segment A000h is the important one. Now, when normally using
  40. this part of memory for graphics output you might think that it is organized as
  41. follows :
  42.                         ┌───────────────────────┐
  43.                OFFSET 0 │. <- pixel 0           │
  44.                         │                       │
  45.                         │                       │
  46.                         │                       │
  47.                         │                       │
  48.                         │                       │
  49.            OFFSET 63999 │        pixel 63999 ->.│
  50.                         └───────────────────────┘
  51.  
  52. What this means is : the VGA memory would be organized as one big chunk of
  53. memory, 64000 bytes long. If you still think this is the case, FORGET IT. 
  54. The VGA memory ISN'T organized like this, but in bitplanes. Those of you who 
  55. have worked with EGA must surely know what programming with bitplanes means. 
  56. Let's take a look at how the VGA memory really is organized :
  57.  
  58.         Bit plane 3     ->       ┌───────────────────────┐ -
  59.         Bit plane 2     ->    ┌──┴────────────────────┐  │ |
  60.         Bit plane 1     -> ┌──┴────────────────────┐  │  │ | each plane
  61.         Bit Plane 0  -> ┌──┴────────────────────┐  │  │  │ | is 64000
  62.                         │                       │  │  │  │ | bytes long
  63.                         │                       │  │  │  │ |
  64.                         │                       │  │  │  │ |
  65.                         │                       │  │  │  │ |
  66.                         │                       │  │  ├──┘ -
  67.                         │                       │  ├──┘ 
  68.                         │                       ├──┘
  69.                         └───────────────────────┘
  70.                         
  71. So, as you can see, the VGA memory consists of four bit planes of 64000 bytes
  72. each, just like the EGA. All four bit planes are mapped at adress A0000h. The
  73. way the bit planes in VGA mode 13h are used differs from the EGA. In EGA modes
  74. the bit planes are used to determine the value of the pixels (0-16). They are 
  75. (in EGA) organized as four 64000 bytes long bit chains (and not byte chains).
  76. In VGA mode 13h they are organised as four byte chains. The four bit planes are
  77. chained together and the pixels are spread over these bit planes. More 
  78. specific : the first pixel = pixel 0 (1 byte) is mapped in bit plane 0 at
  79. offset 0, pixel 1 is mapped in bit plane 1 at offset 0, pixel 2 in bit plane
  80. 2 at offset 0, pixel 3 in bit plane 3 at offset 0, pixel 4 in bit plane 0 at
  81. offset 1 and so on. So far for the VGA mode 13h.
  82.  
  83. A first question that pops up is : how come I don't notice the fact that the
  84. pixels are chained over the four bit planes when using mode 13h ? Well, because
  85. this is done automatically by the VGA card. It looks at the two least signifi-
  86. cant bits in the offset address to determine on which plane the pixel
  87. should be mapped. The offset within this bit plane is the original offset divi-
  88. ded by four. This may sound confusing, so here's an example :
  89. Suppose I want to set the pixel value of the pixel at position (50,50) to 150.
  90. Therefore I would change the byte value of the byte at address A000:3EB2
  91. (offset = 50*320 + 50 = 16050 = 3EB2 hex) to 150. In assembler it would look
  92. about like this :
  93.                         mov     ax,0A000h
  94.                         mov     es,ax
  95.                         mov     di,3EB2h
  96.                         mov     al,150
  97.                         stosb
  98.  
  99. Now, the VGA card will look at the 2 least significant bits in the offset, in
  100. this case 3EB2h = 0011111010110010 binary. These bits are 10 binary = 2, so the
  101. pixel will be mapped in bit plane 2. The offset within this bit plane is
  102. 3EB2h / 4 = 0FACh = 4012.
  103. This automatic mapping is also the reason why you can only access one page in
  104. mode 13h (a page is a piece of memory on the card large enough to contain an
  105. entire screen, so in mode 13h a page is 64000 bytes long) : since the offset
  106. range is limited to 65536, it's impossible to reach the other 3 pages (don't
  107. try using 386 inctruction code to force 32-bits offsets, it won't work).
  108.  
  109.  
  110. Those of you who have tried to make smooth animations such as vectors, sprites
  111. and scrollers know that it's impossible to achieve this when accessing the 
  112. VGA memory directly. A first solution here would be to use a 'virtual page' :
  113. a 64000 bytes long array in central memory in which the screen output is
  114. first built and then copied to the screen. This works but has 2 disadvantages :
  115. it takes 64000 bytes and has to be copied (which takes some time). Therefore,
  116. a multipage system is the best solution : several pages allowing pageswapping
  117. and no time is lost during the swapping. As you already noticed, it's impossi-
  118. ble to achieve this in standard mode 13h (except for some cards, which use seg-
  119. ment B000h as a second page), although there's enough space for four pages.
  120. This is where mode x comes in.
  121.  
  122. Mode x - that's the way to do it
  123. --------------------------------
  124.  
  125. All the previous problems are solved by changing the normal graphics system to
  126. mode x. This is achieved by changing the chain four bit in the memory mode
  127. register (port 3C4/3C5, index 4) from 1 to 0 (on some cards it's necessary to
  128. alter more than one register, but I'll get to this later). What effect does
  129. this have ? Simple, it just disables the automatic mapping and allows the user
  130. to access the bit planes himself. This involves some changes in the way 
  131. graphics output should be used. First, after this bit has been altered, you
  132. gain control over all of the 256000 bytes video memory (4 pages in 320x200x256)
  133. Second, since the automatic mapping is disabled, you have to access the bit
  134. planes yourself (the chained bitplanes system remains, so you have to do exact-
  135. ly what the VGA card does with automatic mapping). Last but not least, you have
  136. to know how to change the current page. We'll get back to this later, first a
  137. sample code which shows how to change to mode x (you have to change to mode
  138. 13h first) :
  139.  
  140.   mov   dx,3C4h                 ; select sequencer registers
  141.   mov   al,4                    ; index 4 -> memory mode register
  142.   out   dx,al                   
  143.   inc   dx
  144.   in    al,dx                   ; read the original value
  145.   and   al,NOT 8                ; turn off chain 4 (bit 3)
  146.   or    al,4                    ; turn off odd/even (bit 2)
  147.   out   dx,al                   ; write the new value
  148.   mov   dx,3CEh                 ; select graphics controller registers
  149.   mov   al,5                    ; index 5 -> mode register
  150.   out   dx,al
  151.   inc   dx
  152.   in    al,dx                   ; read original value
  153.   and   al,NOT 16               ; turn off odd/even (bit 4)
  154.   out   dx,al                   ; write new value
  155.   dec   dx
  156.   mov   al,6                    ; index 6 -> miscellanous register
  157.   out   dx,al                   
  158.   inc   dx
  159.   in    al,dx                   ; read original value
  160.   and   al,NOT 2                ; turn off chain odd/even (bit 1)
  161.   out   dx,al                   ; write original value
  162.  
  163.   mov   dx,3D4h                 ; select CRCT registers
  164.   mov   al,14h                  ; index 14h -> Underline location register
  165.   out   dx,al
  166.   inc   dx
  167.   in    al,dx                   ; read original value
  168.   and   al,NOT 64               ; turn off doubleword (bit 6)
  169.   out   dx,al                   ; write new value
  170.   dec   dx
  171.   mov   al,17h                  ; index 17h -> Mode control register
  172.   out   dx,al
  173.   inc   dx
  174.   in    al,dx                   ; read original value
  175.   or    al,16                   ; turn on byte mode bit (bit 6)
  176.   out   dx,al                   ; write new value
  177.  
  178. As you can see this sample code does nothing exceptional, it just alters cer-
  179. tain bits in certain registers. The odd/even bits have to be changed to ensure
  180. that the way the video memory is accessed is as I described above. Not changing
  181. these bits can screw up the output (I'm not going to explain what these bits do
  182. exactly, but take it from me : change them). The doubleword / word-byte bits
  183. have to be changed to make sure all of the video memory can be accessed. As I
  184. already explained, the chain four bit is the important one, but make sure to
  185. change the others too for compatibility (only changing the chain four bit works
  186. fine on my card, but I know it doesn't on some other cards).
  187.  
  188. Now that we know how to change to mode x, let's continue with the second step,
  189. putting something on the screen (or rather on one of the pages). Let's use the
  190. same example as for the standard mode 13h : suppose I want to change the pixel
  191. value of the pixel at position (50,50) to 150. In normal mode 13h this would
  192. involve changing the value of the byte at address A000:3EB2 to 150. In mode x
  193. this means I have to do what the VGA card normally does for me : change the
  194. byte value at offset 0FACh in bit plane 2 to 150 (look at the previous example
  195. if you don't understand how I found these numbers). This is what it would look
  196. like in assembler :
  197.  
  198.   mov   ax,A000h
  199.   mov   es,ax                   ; select VGA memory segment
  200.   mov   dx,3C4h                 ; select sequencer registers
  201.   mov   al,2                    ; index 2 -> map mask register
  202.   inc   dx
  203.   mov   al,4                    ; change value to 4 : enable plane 2 only
  204.   out   dx,al
  205.   mov   di,0FACh                ; set offset to 0FACh
  206.   mov   al,150                  ; value to write
  207.   stosb
  208.  
  209. Nothing special here either. Now suppose I want to access a different page than
  210. page 1 (say page 3). Quite simple, just increase the offset by 32000. Here's an
  211. overview of the offset ranges for the four pages in mode x :
  212.  
  213.                 page 1 : OFFSET 0     - 15999
  214.                 page 2 : OFFSET 16000 - 31999
  215.                 page 3 : OFFSET 32000 - 47999
  216.                 page 4 : OFFSET 48000 - 63999
  217.  
  218. If you try this out you will notice that it's impossible to see the changes on
  219. the other pages (the current page selection interrupt routine doesn't work).
  220. You have to select the current page through the Start Address. What exactly is
  221. this ? Consider the screen as a window through which the contents of the video
  222. memory can be viewed. In standard mode 13h this screen is as large as the
  223. accessable video memory. In mode x the accessable video memory is four times
  224. larger than as the screen size. The Start Address is used to determine which 
  225. part of the video memory is shown on the screen. Suppose I use the offset 
  226. ranges as shown above and I want to view page 3. Therefore, I would have to
  227. change the Start Address to 32000. To alter the Start Address two registers
  228. have to be changed : Start Address High and Start Addres Low. The reason for
  229. this is the fact that the start addres is an offset and therefore a 16-bit
  230. value, whereas the VGA registers are 8 bits large. Start Address High con-
  231. tains the 8 most significant bits of this 16 bit value and start address low
  232. the 8 least significant bits. Enough blabla; here's an assembler code which
  233. changes the start address value to 32000 (standard value is 0) :
  234.  
  235.   mov   dx,3D4h                 ; select CRCT register
  236.   mov   al,Ch                   ; index 12 -> start address high
  237.   out   dx,al
  238.   inc   dx
  239.   mov   al,125                  ; 32000/256 = 125
  240.   out   dx,al                   ; write new value
  241.   dec   dx
  242.   mov   al,Dh                   ; index 13 -> start address low
  243.   out   dx,al
  244.   inc   dx
  245.   xor   al,al                   ; start address low = 0 (32000 mod 256 = 0)
  246.   out   dx,al                   ; write new value (not really necessary since
  247.                                 ; standard value is 0)
  248.  
  249. That's about all there's to know about the use of mode x. Some things remain
  250. to be told though, especially about certain applications for mode x.
  251.  
  252. Mode x - pro's and con's
  253. ------------------------
  254.  
  255. Now that you know how to use mode x, let's take a look at what can be done with
  256. it. A first and easy application is, of course, a pageswapping routine. Since
  257. more than one page is available pageswapping lies at hand. Only two pages are
  258. needed for this purpose, but the two spare pages can come in handy. A great ad-
  259. vantage here is the fact that, although being non-standard, mode x works on all
  260. VGA cards (as far as I know). This is not the case with the Trident swapping
  261. routine I mentioned earlier (Flame here to the coders who still use this tech-
  262. nique; I know it's easy, but it only works on a quite small number of PC's).
  263.  
  264. Another great use of mode x is a scrolling routine (both vertically and hori-
  265. zontally). By changing the start address by a certain amount continously, you
  266. can scroll through the different pages. Try experimenting with this a bit and
  267. you'll surely get some nice results.
  268.  
  269. Last but not least, mode x is great for combining with other applications such
  270. as split screen and tweak mode (I'll get back to this one in a moment). Again,
  271. experiment and you'll be astounished by the numerous advantages of mode x.
  272.  
  273. Now, after all these advantages you may get a bit too optimistic. Sorry to kill
  274. the mood like this, but there are also some great disadvantages to mode x.
  275. First, suppose you want to display an ordinary 320x200x256 picture in mode x.
  276. Since the pages are spread over 4 bit planes, you'll need to access them (the
  277. bit planes) every time you put a pixel on the screen. For a picture that is
  278. not real-time calculated, there's a simple solution : divide the picture in 4
  279. , one picture for each bit plane. This way you only have to access the map
  280. mask register four times. For other real-time applications such as vectors, it
  281. isn't all this simple. Suppose I want to put a wireframed vector on the screen.
  282. I would have to change the linedrawing routine making it more complex. The most
  283. simple solution is to calculate the new offset/bit plane for every pixel in the
  284. line. This is the easiest solution, but also the slowest. A second solution
  285. is to change the routine so that it draws 4 dotted lines on each bit plane,
  286. reducing the number of map mask register accesses to 4. It's a real drag to
  287. code, but it's the fastest way. A third possibility is to draw real-time pictu-
  288. res on a 'virtual page' and then overlay it on the video pages. This is also
  289. easy but slow. As you can see, I haven't figured out a good solution for this
  290. problem, so if you have, please do let me know.
  291.  
  292. Mode x, mode y, tweaked mode, etc ...
  293. -------------------------------------
  294.  
  295. As I mentioned before, mode x works on all VGA cards - although being non-
  296. standard - but as I also mentioned before, it means something different to
  297. some other coders. This bring us to mode y and tweaked mode. I can't really
  298. tell what mode y is exactly, but I guess it's similar to tweaked mode (if some-
  299. one knows what it is, let me know). Tweaked mode probably sounds more familiar
  300. to most of you. It's also a non-standard mode (or rather modes) which also
  301. works on all VGA cards and allows resolutions such as 320x400, 360x480 and so
  302. on. The problem here is that tweaked mode implies mode x, since it's necessary
  303. to access more than 64000 bytes of video memory. This makes it all a bit con-
  304. fusing and I guess mode x and tweaked mode are one and the same to a lot of
  305. coders. There are lots of sources available for mode x and tweaked mode, but
  306. they are (unfortunately) almost always combined. I didn't find any file explai-
  307. ning how mode x works with a 320x200 resolution. This is also the reason why I
  308. wrote this textfile. I sure hope it helped you out and saved you the time it
  309. took me to find out how mode x works. If you want to find out more about
  310. tweaked mode, I would advise you to get one of the so many files available on
  311. most BBS's (such as The Graphics Engine or VGAkit).
  312.  
  313. Epilogue
  314. --------
  315.  
  316. Yep, this is about it. There isn't anything left to tell about mode x - at
  317. least nothing I can think about. If you do have questions or experienced pro-
  318. blems with this text, be free to let me know. Try not to ask too simple 
  319. questions such as "what's a pixel ?" or "hey, I disassembled my mouse but
  320. didn't find the VGA card", I won't respond to those. Don't ask me to teach you
  321. everything on the VGA card or on assembler either, just things that concern
  322. mode x. You can try to contact me concerning coding in general too. Here's
  323. where you can reach me :
  324.  
  325. BBS's :
  326. -------
  327.  
  328. ZEUS :  +32-(0)9-264.47.51 (refer to THOMAS VIDTS)
  329. BUSTER : node 1 : +32-(0)53-77.23.47 (refer to RETARD ED)
  330.  
  331. INTERNET ADDRESS : VIDTS@WET.RUG.AC.BE
  332.